package com.lanyang.mp.genertor;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.builder.GeneratorBuilder;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.mysql.cj.jdbc.MysqlDataSource;

/**
 * @author liaochengli
 * @date 2024/6/3 13:14
 * @des
 */
public class CodeGenerator {


    public static void main(String[] args) {

        //文件保存路径
        String targetDir = "E:\\code";
        String author = "lanyang";
        //包路径
        String parentPackage = "com.lanyang.framework.mp.module";
        //模块名称
        String moduleName = "biz";
        String tableName = "user_info";

        //数据库配置
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setURL("jdbc:mysql://137.0.0.1:3306/biz");
        dataSource.setUser("system");
        dataSource.setPassword("123456");
        DataSourceConfig dsc = new DataSourceConfig.Builder(dataSource).build();

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator(dsc);

        // 全局配置
        GlobalConfig gc = GeneratorBuilder.globalConfigBuilder()
                .outputDir(targetDir)
                .fileOverride()
                .author(author)
                .commentDate("yyyy-MM-dd")
                .enableSwagger()
                .dateType(DateType.ONLY_DATE)
                .build();

        mpg.global(gc);


        // 包配置 parent + moduleName 为类路径
        PackageConfig pc = GeneratorBuilder.packageConfigBuilder()
                .parent(parentPackage)
                .moduleName(moduleName)
                .build();
        mpg.packageInfo(pc);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig.Builder()
                //指定service 的模板路径，不指定则适用默认的模板
                //.service("templates/service.java.vm", "templates/serviceImpl.java.vm")
                .entity("templates/entity.java.vm")
                .build();

        mpg.template(templateConfig);

        StrategyConfig strategyConfig = GeneratorBuilder.strategyConfigBuilder().addInclude(tableName).build();
        //实体类策略
        strategyConfig.entityBuilder()
//                .superClass("com.lanyang.framework.mp.module.BasePlainEntity")
                .enableLombok()
                .columnNaming(NamingStrategy.underline_to_camel)
//                .addSuperEntityColumns("create_time", "create_by", "update_time", "update_by", "remark")
//                .addSuperEntityColumns("create_time", "create_by", "update_time", "update_by")
                .naming(NamingStrategy.underline_to_camel);

        //mapper策略 生成数据列和映射关系
        strategyConfig.mapperBuilder().enableBaseColumnList().enableBaseResultMap();

        // 配置 Service 接口和实现类的命名规则（去掉I前缀）// Service接口命名：%s 代表实体名（如 User → UserService）
        strategyConfig.serviceBuilder().formatServiceFileName("%sService");

        //controller设置为RestController
        strategyConfig.controllerBuilder().enableRestStyle();

        mpg.strategy(strategyConfig);

        mpg.execute();
    }

}